package com.guoheng.hazard.sys.service.impl.hazard;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.enums.ProcessEnum;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.mapper.hazard.KeyRegulatoryProcessMapper;
import com.guoheng.hazard.sys.model.dto.entpr.KeyRegulatoryProcessSelectDTO;
import com.guoheng.hazard.sys.model.dto.hazard.KeyRegulatoryProcessInsertDTO;
import com.guoheng.hazard.sys.model.dto.hazard.KeyRegulatoryProcessUpdateDTO;
import com.guoheng.hazard.sys.model.dto.hazard.ProcessesStatisticsDTO;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.model.entity.hazard.KeyRegulatoryProcess;
import com.guoheng.hazard.sys.service.hazard.KeyRegulatoryProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 类功能描述:　KeyRegulatoryProcessServiceImpl
 *
 * @author fbl
 * @date 2019-24-13 8:24
 */
@Service
public class KeyRegulatoryProcessServiceImpl implements KeyRegulatoryProcessService {

    @Resource
    private KeyRegulatoryProcessMapper keyRegulatoryProcessMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;

    @Override
    public Result getProcesses(String entprName, String processName, Long entprId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<KeyRegulatoryProcessSelectDTO> keyRegulatoryProcessSelectDTOS = new ArrayList<>();
        List<KeyRegulatoryProcess> processes = keyRegulatoryProcessMapper.getProcesses(entprName, processName, entprId);

        // 得到企业姓名
        for (KeyRegulatoryProcess process : processes) {
            KeyRegulatoryProcessSelectDTO keyRegulatoryProcessSelectDTO = new KeyRegulatoryProcessSelectDTO();
            BeanUtils.copyProperties(process, keyRegulatoryProcessSelectDTO);
            Long eId = process.getEntprId();
            EntprBasicInfo entprBasicInfoById = entprBasicInfoMapper.getEntprBasicInfoById(eId);
            keyRegulatoryProcessSelectDTO.setEntprName(entprBasicInfoById.getEntprName());
            keyRegulatoryProcessSelectDTOS.add(keyRegulatoryProcessSelectDTO);
        }

        PageInfo<KeyRegulatoryProcessSelectDTO> keyRegulatoryProcessPageInfo = new PageInfo<>(keyRegulatoryProcessSelectDTOS);
        return Result.success(keyRegulatoryProcessPageInfo);
    }

    @Override
    public Result updateProcesses(KeyRegulatoryProcessUpdateDTO keyRegulatoryProcessUpdateDTO) {
        KeyRegulatoryProcess keyRegulatoryProcess = new KeyRegulatoryProcess();
        BeanUtils.copyProperties(keyRegulatoryProcessUpdateDTO, keyRegulatoryProcess);
        Integer processType = keyRegulatoryProcess.getProcessType();
        ProcessEnum[] values = ProcessEnum.values();
        for (ProcessEnum value : values) {
            if (processType.equals(value.getCode())) {
                keyRegulatoryProcess.setProcessName(value.getMsg());
            }
        }
        keyRegulatoryProcessMapper.updateProcess(keyRegulatoryProcess);
        return Result.success();
    }

    @Override
    public Result insertProcesses(KeyRegulatoryProcessInsertDTO keyRegulatoryProcessInsertDTO) {
        KeyRegulatoryProcess keyRegulatoryProcess = new KeyRegulatoryProcess();
        BeanUtils.copyProperties(keyRegulatoryProcessInsertDTO, keyRegulatoryProcess);
        Integer processType = keyRegulatoryProcess.getProcessType();
        ProcessEnum[] values = ProcessEnum.values();
        for (ProcessEnum value : values) {
            if (processType.equals(value.getCode())) {
                keyRegulatoryProcess.setProcessName(value.getMsg());
            }
        }
        keyRegulatoryProcessMapper.insertProcess(keyRegulatoryProcess);
        return Result.success();
    }

    @Override
    public Result deleteProcesses(Long id) {
        KeyRegulatoryProcess keyRegulatoryProcess = new KeyRegulatoryProcess();
        keyRegulatoryProcess.setId(id);
        keyRegulatoryProcess.setDelFlag(true);
        keyRegulatoryProcessMapper.updateProcess(keyRegulatoryProcess);
        return Result.success();
    }

    @Override
    public Result selectProcessesById(Long id) {
        return Result.success(keyRegulatoryProcessMapper.selectProcessesById(id));
    }

    @Override
    public Result processesStatistics() {
        List<Integer> types = new ArrayList<>();
        // 查询工艺类型和数量
        List<ProcessesStatisticsDTO> ProcessesStatistics = keyRegulatoryProcessMapper.getProcessesTypeAndNum();
        // 查询拥有此高危工艺的企业名称
        for (ProcessesStatisticsDTO processesType : ProcessesStatistics) {
            Integer type = processesType.getProcessesType();
            types.add(type);
            processesType.setEntprName(keyRegulatoryProcessMapper.getEntprName(type));
        }

        // 共有18种高危工艺
        for (int i = 1; i < 19; i++) {
            if(!types.contains(i)){
                ProcessesStatisticsDTO processesStatisticsDTO = new ProcessesStatisticsDTO();
                processesStatisticsDTO.setProcessesType(i);
                processesStatisticsDTO.setProcessesNum(0);
                processesStatisticsDTO.setEntprName(new ArrayList<>());
                ProcessesStatistics.add(processesStatisticsDTO);
            }
        }
        return Result.success(ProcessesStatistics);
    }
}
